package com.woniuxy.oldmanapi.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.Card;
import com.woniuxy.oldmanapi.entity.Discharge;
import com.woniuxy.oldmanapi.entity.Othercost;
import com.woniuxy.oldmanapi.mapper.CardMapper;
import com.woniuxy.oldmanapi.mapper.DischargeMapper;
import com.woniuxy.oldmanapi.zvo.DischargeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DischargeService {
    @Autowired
    DischargeMapper dischargeMapper;
    @Autowired
    CardMapper cardMapper;

    public PageInfo<DischargeVo> selectDischarge(int now, int size, String name) {
        PageHelper.startPage(now,size);
        List<DischargeVo> consulterList = dischargeMapper.selectConsulterByName(name);
        PageInfo<DischargeVo> pageInfo = new PageInfo<>(consulterList);
        return pageInfo;
    }

    public void updateDischarge(Discharge consulter) {
        dischargeMapper.updateByPrimaryKey(consulter);
    }

    public void deleteDischarge(int id) {
        dischargeMapper.deleteByPrimaryKey(id);
    }
    @Transactional
    public void settlementDischarge(int id) {
        Discharge discharge = dischargeMapper.selectByPrimaryKey(id);
        for (Card card : cardMapper.selectCardById(discharge.getOlderId())) {
            cardMapper.logout(card.getId());
        }
        dischargeMapper.settlementDischarge(id);

    }

    public void insertDischarge(Discharge consulter) {
        consulter.setStatus(0);
        dischargeMapper.insert(consulter);
    }

    public DischargeVo selectDischargeById(int id) {
        return dischargeMapper.selectDischargeById(id);
    }
}
